﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace CRMSalesManagement.Manage.FrontPageManagement
{
    public partial class NewsAdd : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            this.lblSaveResult.Text = "";
            if (!IsPostBack)
            {
                this.txtTitle.Focus();
            }
        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            this.lblAuthor.Text = "";
            this.lblContent.Text = "";
            this.lblSaveResult.Text = "";
            this.lblTitle.Text = "";
            if (this.txtTitle.Text == "")
            {
                this.lblTitle.Text = "文章标题不能为空";
            }
            else
            {
                if (this.txtAuthor.Text == "")
                {
                    this.lblAuthor.Text = "供稿人不能为空";
                }
                else
                {
                    //if (this.txtAuthor.Text == "")
                    //{
                    //    this.lblAuthor.Text = "供稿人不能为空";
                    //}
                    //else
                    //{
                    if (this.content3.InnerText.Trim() == "")
                    {
                        this.lblContent.Text = "内容不能为空";
                    }
                    else
                    {
                        CRM.DataLayer.ContentInfor newsData =new CRM.DataLayer.ContentInfor();
                        newsData.author = txtAuthor.Text;
                        newsData.content = content3.Value.Trim();
                        newsData.title = txtTitle.Text;
                        newsData.date = DateTime.Now;
                        //newsData.type = Session["type"].ToString();
                        if (Session["type"] != null)
                        {
                            newsData.type = Session["type"].ToString();

                        }
                        else
                        {
                            return;
                        }
                        newsData.image = setImgUrl();
                        bool checkNews =CRM.Business.NewList.AddNews(newsData);
                        if (!checkNews)
                        {
                            this.lblSaveResult.Text = "保存失败";
                        }
                        else
                        {
                            string type = Session["type"].ToString();
                            Session["type"] = null;
                            Response.Redirect("ManageNewsList.aspx?type=" + type);
                        }
                    }
                }//endif
            }//endif
        }

        /// <summary>
        /// 存图片的URL
        /// </summary>
        /// <returns></returns>
        private string setImgUrl()
        {
            try
            {
                string str = content3.Value.Trim();
                int start = str.IndexOf("src", str.IndexOf("<img", 0) + 3) + 5;
                int end = str.IndexOf("/>", start) - 2;
                return str.Substring(start, end - start);
            }
            catch
            {
                return "";
            }
        }

        protected void btnReset_Click(object sender, EventArgs e)
        {
            this.txtAuthor.Text = "";
            this.txtTitle.Text = "";
            this.content3.Value = "";
            this.lblSaveResult.Text = "";
        }
    }
}